#!/usr/bin/bash
currentDir=`pwd`

packagePath="/root/BaseOS/Packages/"

function download_debuinfo {

    rpmlist=`ls $packagePath |grep -v i686.rpm |grep -v noarch.rpm |grep .rpm`
    for item in $rpmlist
    do
        
        libsize=`rpm -qpl $packagePath$item | grep ^/usr/lib/[^/]*\.so |wc -l`
        lib64size=`rpm -qpl $packagePath$item | grep ^/usr/lib64/[^/]*\.so |wc -l`
        libEx=`rpm -qpl $packagePath$item | grep ^/lib/[^/]*\.so |wc -l`
        lib64Ex=`rpm -qpl $packagePath$item | grep ^/lib64/[^/]*\.so |wc -l`
        echo "开始处理"： $item  ${libsize} ${lib64size} ${libEx} ${lib64Ex}
        if [ "${libsize}" != "0" ] || [ "${lib64size}" != "0" ] || [ "${libEx}" != "0" ] || [ "${lib64Ex}" != "0" ];then
            echo "开始处理"： $item
            name=`rpm -qpi $packagePath$item 2>/dev/null |grep "Name   " |awk '{print $3}'`
            version=`rpm -qpi $packagePath$item 2>/dev/null |grep "Version   " |awk '{print $3}'`
            release=`rpm -qpi $packagePath$item 2>/dev/null |grep "Release   " |awk '{print $3}'`
            architecture=`rpm -qpi $packagePath$item 2>/dev/null |grep "Architecture:" |awk '{print $2}'`
            echo "name:" $name
            if [ "${name}" = "kernel" ] || [ "${name}" = "kernel-debug" ] || [ "${name}" = "firefox" ] || [ "${name}" = "mariadb" ] || [ "${name}" = "webkitgtk3" ] || [ "${name}" = "xulrunner" ]; then  
                echo "skip $name"
            else
                rpm2cpio $packagePath$item | cpio -div --no-absolute-filenames -D $currentDir"/vsroot"
                echo $name "-" $version "-" $release
                debugname=$name"-debuginfo-"$version"-"$release"."$architecture
                echo "debugname:" $debugname
                echo "down yum install -y --downloadonly --downloaddir=$currentDir"/debuginfo" --disablerepo=* --enablerepo=Debuginfo $debugname"
                yum install -y --downloadonly --downloaddir=$currentDir"/debuginfo" --disablerepo=* --enablerepo=debug $debugname
            fi
        fi
    done
}

function udecompression {

    debuglist=`ls $currentDir"/debuginfo"`

    for item in $debuglist
    do
        echo "开始解压:"$item
        rpm2cpio $currentDir"/debuginfo/"$item |cpio -div --no-absolute-filenames -D $currentDir"/vsroot" 
    done

}

function convert_sodebug2dump {

    cp /root/dump-maker.py $currentDir/vsroot
    cd $currentDir/vsroot
    python3 dump-maker.py
}


download_debuinfo

udecompression

convert_sodebug2dump



